package com.ruoyi.web.controller.comlbs;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.FtpConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.FileObject;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.file.FtpUtil;
import com.ruoyi.system.domain.File;
import com.ruoyi.system.service.IFileService;
import com.ruoyi.system.utils.ComLbsUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.lang.System.in;

/**
 * 文件管理Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/file")
public class FileController extends BaseController {
    @Autowired
    private IFileService fileService;
    @Autowired
    private FtpUtil ftpUtil;
    @Autowired
    private FtpConfig ftpConfig;

    @ApiOperation("文件管理列表")
    @ApiOperationSupport(includeParameters = {"fileName", "uploadType", "createUserName"})
    @GetMapping("/list")
    public TableDataInfo list(File file) {
        startPage();
        List<File> list = fileService.selectList(file);
        return getDataTable(list);
    }

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fileService.getById(id));
    }

    @ApiOperation("新增文件")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody File file) {
        return toAjax(fileService.save(file));
    }

    @ApiOperation("编辑文件")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody File file) {
        ComLbsUtils.fileHandle(fileService.getById(file.getId()).getFileAddress(), file.getFileAddress(), true);
        return toAjax(fileService.updateById(file));
    }

    @ApiOperation("删除文件")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            File file = fileService.getById(id);
            String path = file.getFileAddress().get(0).getPath();
            if (ftpUtil.deleteFile(ftpConfig.getPrefixPath() + path)) {
                fileService.removeById(id);
            }
        }
        return success();
    }

    @ApiOperation("FTP文件上传")
    @Log(title = "FTP文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestPart MultipartFile[] file, @RequestParam Integer uploadType) {
        String uploadTypeName = DictUtils.getDictLabel("uploadType", uploadType);
        String uploadFilePath = ftpConfig.getFtpFilePath() + "/" + uploadTypeName;
        return success(uploadFileFTP(file, uploadFilePath, false));
    }
}
